package com.example.kugou.common.music;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.id3.ID3v23Frame;

import java.io.File;
import java.sql.Time;

public class Mp3Info {
        //音乐名
        private String songName;
        //歌手名
        private String singerName;
        //专辑名
        private String album;
        //时长
        private long duration;


        private int singerId;

    public Mp3Info(Music music) {
        if(music != null){
            this.songName = music.getSongName();
            this.singerName = music.getArtist();
            this.duration = music.getTotalTimeStr();
            this.album = music.getAlbum();
            this.musicPath = music.getMusicPath();
        }
    }


    public int getSingerId() {
        return singerId;
    }

    public void setSingerId(int singerId) {
        this.singerId = singerId;
    }

    /**
     * 视频文件的相对路径
     */
    private String musicPath;

    private String lrcPath;

    public String getMusicPath() {
        return musicPath;
    }

    public void setMusicPath(String musicPath) {
        this.musicPath = musicPath;
    }

    public String getLrcPath() {
        return lrcPath;
    }

    public void setLrcPath(String lrcPath) {
        this.lrcPath = lrcPath;
    }

    public String getSongName() {
            return songName;
        }

        public void setSongName(String songName) {
            this.songName = songName;
        }

        public String getSingerName() {
            return singerName;
        }

        public void setSingerName(String singerName) {
            this.singerName = singerName;
        }

        public String getAlbum() {
            return album;
        }

        public void setAlbum(String album) {
            this.album = album;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        public Mp3Info() {
        }






        public Mp3Info(String songName, String singerName, String album, long duration) {
            this.songName = songName;
            this.singerName = singerName;
            this.album = album;
            this.duration = duration;
        }

    @Override
    public String toString() {
        return "Mp3Info{" +
                "songName='" + songName + '\'' +
                ", singerName='" + singerName + '\'' +
                ", album='" + album + '\'' +
                ", duration=" + duration +
                ", musicPath='" + musicPath + '\'' +
                ", lrcPath='" + lrcPath + '\'' +
                '}';
    }

    /**
     * @param filePath
     * @Description: 传入MP3文件地址，读取mp3文件信息
     * @Return: com.t.test.TestAll
     * @Auther: admin
     * @Date 2018/12/30 17:34
     */

    public static Mp3Info getSongInfo(String filePath) {
        Mp3Info mp3Info = null;

        try {
//         读取文件信息
            MP3File mp3File = (MP3File) AudioFileIO.read(new File(filePath));
//            获取头
            MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();

            //歌名
            ID3v23Frame songnameFrame = (ID3v23Frame) mp3File.getID3v2Tag().frameMap.get("TIT2");
            String songName = songnameFrame.getContent();
            //歌手
            ID3v23Frame artistFrame = (ID3v23Frame) mp3File.getID3v2Tag().frameMap.get("TPE1");
            String artist = artistFrame.getContent();
            //专辑
            ID3v23Frame albumFrame = (ID3v23Frame) mp3File.getID3v2Tag().frameMap.get("TALB");
            String album = albumFrame.getContent();
            //时长
            int duration = audioHeader.getTrackLength();

            mp3Info = new Mp3Info(songName, artist, album, duration);

        } catch (Exception e) {
            e.printStackTrace();
            try {
                Music music = MusicUtile.getMusicInfo(filePath);
                mp3Info = new Mp3Info(music);
                System.out.println(mp3Info);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        System.out.println(mp3Info);
        return mp3Info;
    }

    /**
     * 秒转换为指定格式的日期
     *
     * @param second
     * @return
     */
    public static Time secondToDate(int second) {
        //转换为毫秒,但需要减去最基础的8小时
        Time time = new Time(second * 1000 - 8 * 60 * 60 * 1000);
        return time;
    }


    public static void main(String[] args) {
        Mp3Info songInfo = new Mp3Info().getSongInfo("G:\\小组项目\\网易云\\网易云\\163mHome\\audio\\22222.mp3");
        System.out.println(songInfo);
    }
}
